package com.sh.caryr.erp.controller.stock;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.sh.caryr.erp.entity.ResultMessage;
import com.sh.caryr.erp.entity.product.CombDetail;
import com.sh.caryr.erp.entity.product.CombNum;
import com.sh.caryr.erp.entity.product.Product;
import com.sh.caryr.erp.entity.stock.Sku;
import com.sh.caryr.erp.entity.enums.ProductTypeEnum;
import com.sh.caryr.erp.service.IProductService;
import com.sh.caryr.erp.service.ISkuService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/stock")
public class StockController {

    @Autowired
    private IProductService productService;
    @Autowired
    private ISkuService skuService;

    @ApiOperation(value = "产品库存信息", notes = "产品库存信息")
    @ApiImplicitParam(name = "productType", value = "产品类型", required = true, dataType = "int")
    @RequestMapping(value = "/{productType}", method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    public ResultMessage<List<Product>> findStockByType(@PathVariable int productType) {
        List<Product> products = productService.selectList(new EntityWrapper<Product>().eq("type", productType));

        for (Product product : products) {
            productService.getProductDetail(product);

            if (product.getType() == ProductTypeEnum.NORMAL) {
                //普通商品库存
                List<Sku> skuList = product.getSkuList();
                int usableAmount = 0;
                int notGoodAmont = 0;
                int lockedAmont = 0;
                for (Sku sku : skuList) {
                    usableAmount += sku.getUsableAmount();
                    notGoodAmont += sku.getNotGoodAmount();
                    lockedAmont += sku.getLockedAmount();
                }
                product.setTotalAmount(usableAmount);
                product.setNotGoodAmount(notGoodAmont);
                product.setLockedAmount(lockedAmont);
            } else {
                //套件商品库存
                List<CombDetail> combDetailList = product.getCombDetailList();
                int productTotalAmont = 0;
                for (CombDetail combDetail : combDetailList) {
                    float combTotalAmont = Integer.MAX_VALUE;

                    for (CombNum combNum : combDetail.getCombNumList()) {
                        Sku sku = skuService.selectById(combNum.getSkuId());

                        float skuAvailableNum = (sku.getUsableAmount() - sku.getLockedAmount() - sku.getNotGoodAmount())
                                / combNum.getSkuNum();

                        combTotalAmont = combTotalAmont >= skuAvailableNum ? skuAvailableNum : combTotalAmont;
                    }

                    combDetail.setTotalAmount(combTotalAmont);
                    combDetail.setNotGoodAmount(0f);
                    combDetail.setLockedAmount(0f);
                    productTotalAmont += combTotalAmont;
                }
                product.setTotalAmount(productTotalAmont);
                product.setNotGoodAmount(0);
                product.setLockedAmount(0);
            }
        }

        return new ResultMessage<>(products);
    }
}
